﻿using msilgc.cbuilder.CSyntaxTree;
using msilgc.cbuilder.MSILSyntaxTree;
using msilgc.CSyntaxTree;

namespace msilgc.cbuilder.CBuilder {
    internal class DupTranslator : ITranslator {
        private readonly VariableFactory _varFactory;

        public DupTranslator(VariableFactory varFactory) {
            _varFactory = varFactory;
        }

        public ICStatement TranslateAndAdd(CMethodBody methodBody, CBasicBlock currentBlock, BasicBlock block, Instruction instruction) {
            IVariable source = EvaluationStackHelper.PopVariableOffEvaluationStack(currentBlock);
            IVariable target1 = _varFactory.CreateNext(source);
            IVariable target2 = _varFactory.CreateNext(source);
            AssignToLocalVariable assignStm1 = new AssignToLocalVariable(source, target1);
            AssignToLocalVariable assignStm2 = new AssignToLocalVariable(source, target2);
            currentBlock.Statements.Add(assignStm1);
            currentBlock.Statements.Add(assignStm2);
            return null;
        }
    }
}
